package com.eatwhat.mapper;

import com.eatwhat.entity.Food;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface FoodMapper {
    
    @Select("SELECT f.*, c.name as category_name FROM food f " +
            "LEFT JOIN category c ON f.category_id = c.id " +
            "ORDER BY f.id DESC LIMIT #{offset}, #{limit}")
    List<Food> findByPage(@Param("offset") int offset, @Param("limit") int limit);
    
    @Select("SELECT COUNT(*) FROM food")
    long countTotal();
    
    @Select("SELECT f.*, c.name as category_name FROM food f " +
            "LEFT JOIN category c ON f.category_id = c.id " +
            "WHERE f.status = 1")
    List<Food> findAllEnabled();
    
    @Select("SELECT f.*, c.name as category_name FROM food f " +
            "LEFT JOIN category c ON f.category_id = c.id " +
            "WHERE f.id = #{id}")
    Food findById(Long id);
    
    @Select("SELECT f.*, c.name as category_name FROM food f " +
            "LEFT JOIN category c ON f.category_id = c.id " +
            "WHERE f.category_id = #{categoryId} AND f.status = 1")
    List<Food> findByCategoryId(Long categoryId);
    
    @Insert("INSERT INTO food(name, description, image_url, category_id, price, " +
            "difficulty, cook_time, ingredients, steps, tags, status, weight) " +
            "VALUES(#{name}, #{description}, #{imageUrl}, #{categoryId}, #{price}, " +
            "#{difficulty}, #{cookTime}, #{ingredients}, #{steps}, #{tags}, #{status}, #{weight})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Food food);
    
    @Update("UPDATE food SET name = #{name}, description = #{description}, " +
            "image_url = #{imageUrl}, category_id = #{categoryId}, price = #{price}, " +
            "difficulty = #{difficulty}, cook_time = #{cookTime}, ingredients = #{ingredients}, " +
            "steps = #{steps}, tags = #{tags}, status = #{status}, weight = #{weight} " +
            "WHERE id = #{id}")
    int update(Food food);
    
    @Delete("DELETE FROM food WHERE id = #{id}")
    int deleteById(Long id);
    
    @Select("SELECT f.*, c.name as category_name FROM food f " +
            "LEFT JOIN category c ON f.category_id = c.id " +
            "WHERE f.name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "f.description LIKE CONCAT('%', #{keyword}, '%') OR " +
            "f.tags LIKE CONCAT('%', #{keyword}, '%')")
    List<Food> searchByKeyword(String keyword);
}